<template>
  <div style="width: 100%">
    <a-card class="card" :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="院系">
                <a-select showSearch optionFilterProp="children" v-model="queryParam.ssyxm" @change="yxChange" :allowClear="!user.departmentId">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+"-"+ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="招生年度">
                <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zsxn">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :sm="{span:24}" style="text-align:center">
              <a-button type="primary" @click="handleCreate">添加</a-button>
              <a-button type="primary" @click="handleReduction" :disabled="!data.length">还原为未上报状态</a-button>
              <a-button type="primary" @click="loadData">查询</a-button>
              <!-- <a-button type="primary" @click="handleExport">导出</a-button> -->
              <a-button type="primary" @click="exportPDF">导出PDF</a-button>
              <a-button type="primary" @click="exportBDF">导出专业DBF</a-button>
              <a-button type="primary" @click="handleBatchDelete" v-hasPermission="'sszbzymlgl:allDelete'">批量删除</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
       <a-alert banner message="温馨提示：本页面请选择院系后再导出pdf，清空院系，直接点击bdf，导出全部院系数据" style="margin-bottom:20px"/>
    <a-card class="card" :bordered="false">
      <a-table ref="table" size="default" rowKey="key" :columns="columns" :loading="loading" 
        :dataSource="data" :pagination="pagination" @change="handleTableChange">
        <template slot="action" slot-scope="text,record">
          <a @click="record.sbzt!='1' && handleEdit(record.id)" :style="record.sbzt!='1'?'':'color:#999'">修改</a>
          <a @click="record.sbzt!='1' && handleDelete(record.id)" :style="record.sbzt!='1'?'':'color:#999'">删除</a>
        </template>

        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
      </a-table>
      <!-- 新增 -->
      <directory-add ref="collegeAdd" @close="handleCreateClose" @success="handleCreateSuccess" :addVisiable="addVisiable"></directory-add>
      <!-- 编辑 -->
      <directory-edit ref="collegeEdit" @close="handleEditClose" @success="handleEditSuccess" :info="editData"
        :editVisible="editVisible"></directory-edit>
    </a-card>
  </div>
</template>

<script>
  // import moment from 'moment'
  import directoryAdd from './professionalAdd'
  import directoryEdit from './professionalEdit'
  import {
    STable
  } from '~'
  import {
    mapState
  } from 'vuex'

  const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']

  export default {
    components: {
      STable,
      directoryEdit,
      directoryAdd
    },
    data() {
      return {
        loading: false,
        data: [],
        // 查询参数
        queryParam: {
          ssyxm: '',
          zsxn: '',
          pageNum: 1
        },
        postParams: {},
        user:{},
        // 表头
        columns: [{
            title: '序号',
            scopedSlots: {
              customRender: 'serial'
            }
          },
          {
            title: '专业代码',
            dataIndex: 'zydm'
          },
          {
            title: '专业',
            dataIndex: 'sszym'
          },
          {
            title: '招生人数',
            dataIndex: 'zsrs'
          },
          {
            title: '考试科目',
            dataIndex: 'kskm'
          },
          {
            title: '复试科目',
            dataIndex: 'fskm'
          },
          {
            title: '加试科目',
            dataIndex: 'jskm'
          },
          {
            title: '备注',
            dataIndex: 'note'
          },
          {
            title: '操作',
            dataIndex: 'cz',
            fixed: 'right',
            width: 120,
            scopedSlots: {
              customRender: 'action'
            }
          }
        ],
        selectedRowKeys: [],
        selectedRows: [],
        options: {
          alert: {
            show: true,
            clear: () => {
              this.selectedRowKeys = []
            }
          },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: (selectedRowKeys, selectedRows) => {
              console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
              this.selectedRowKeys = selectedRowKeys
              this.selectedRows = selectedRows
            },
            onSelect: (record, selected, selectedRows) => {
              console.log(record, selected, selectedRows)
            },
            onSelectAll: (selected, selectedRows, changeRows) => {
              console.log(selected, selectedRows, changeRows)
            },
          }
        },
        addVisiable: false,
        editVisible: false,
        selectMenu: {
          yx: [] // 院系
        },
        editData: {},
        pagination: {
          current: 1
        }
      }
    },
    computed: {
      ...mapState({
        systemInfo: state => state.app.systemInfo,
        TYPE0126: state => state.app['TYPE0126'] // 年级
      })
    },
    created() {
      this.initSelectMenu()
      selectArr.forEach(v => {
        this.$store.dispatch('app/setDictionary', v)
      })
      this.user = this.$db.get('USER')
    },
    methods: {
      initSelectMenu() {
        let allRequest = [
          this.$api.base.collegeAll()
        ]
        Promise.all(allRequest).then(res => {
          this.selectMenu = {
            yx: res[0].data
          }
          this.queryParam.ssyxm = res[0].data[0].dwh
          // 获取当前学年
          this.$api.base.getSchoolYear().then(res => {
            this.queryParam.zsxn = res.data.masterAdmissionYear
            this.loadData({
              pageSize: 10,
              pageNum: 1
            })
          })
        })
      },
      // 加载数据
      loadData(parameter = {}) {
        if (!parameter.pageNum) {
          this.pagination.current = 1
          parameter.pageNum = 1
        }
        let params = Object.assign(this.queryParam, parameter)
        this.loading = true;
        this.$api.recruitstat.professCatalogManageList(params).then(res => {
          // console.log(res)
          const pagination = { ...this.pagination
          };
          pagination.total = 0
          if (res.data && res.data.total) {
            pagination.total = res.data.total;
          }
          this.data = []
          this.pagination = pagination;
          if (res.data && res.data.rows.length) {
            this.data = res.data.rows;
          }
          this.loading = false;
        }).finally(() =>{
        this.loading = false;
      })
      },
      // 分页
      handleTableChange(pagination, filters, sorter) {
        console.log(pagination);
        const pager = { ...this.pagination
        };
        pager.current = pagination.current;
        this.pagination = pager;
        this.loadData({
          pageSize: 10,
          pageNum: pagination.current
        });
      },
      // 多选
      onSelectChange(selectedRowKeys, selectedRows) {
        console.log('selectedRowKeys changed: ', selectedRowKeys);
        this.selectedRows = selectedRows
        this.selectedRowKeys = selectedRowKeys
      },
      // 选择院系
      yxChange(code) {},
      // 选择招生年度
      zsndChange() {

      },
      // 添加
      handleCreate() {
        this.addVisiable = true
      },
      // 关闭添加弹层
      handleCreateClose() {
        this.addVisiable = false
        this.loadData({
          pageSize: 10,
          pageNum: this.pagination.current
        })
      },
      // 添加成功
      handleCreateSuccess() {
        this.addVisiable = false
        this.$message.success('添加成功')
        this.loadData({
          pageSize: 10,
          pageNum: this.pagination.current
        })
      },
      // 修改
      handleEdit(key) {
        this.$api.recruitstat.setUpBySpecialtyId(key).then(res => {
          this.editData = res.data
          this.editVisible = true
        })
      },
      // 关闭添加弹层
      handleEditClose() {
        this.editVisible = false
        this.loadData({
          pageSize: 10,
          pageNum: this.pagination.current
        })
      },
      // 添加成功
      handleEditSuccess() {
        this.editVisible = false
        this.$message.success('添加成功')
        this.loadData({
          pageSize: 10,
          pageNum: this.pagination.current
        })
      },
      // 导出
      exportExcel() {
        console.log('导出')
      },
      // 删除
      handleDelete(key) {
        console.log('删除')
        var that = this;
        this.$confirm({
          title: '确定删除?',
          onOk() {
            console.log('OK');
            that.deleteData(key)
          },
          onCancel() {
            console.log('Cancel');
          },
          class: 'test',
        });
      },
      // 批量删除
      handleBatchDelete() {
        console.log('批量删除')
        var deleteId = ''
        if (!this.selectedRowKeys.length) {
          this.$message.warning('请选择需要删除的数据')
          return
        }
        if (this.selectedRows && this.selectedRows.length) {
          deleteId = this.selectedRows.map(v => {
            return v.id
          }).toString()
        }
        console.log(deleteId)
        var that = this;
        this.$confirm({
          title: '确定删除?',
          onOk() {
            console.log('OK');
            that.deleteData(deleteId)
          },
          onCancel() {
            console.log('Cancel');
          },
          class: 'test',
        });
      },
      deleteData(code) {
        this.$api.recruitstat.setUpBySpecialtyDelete(code).then(res => {
          this.loadData({
            pageSize: 10,
            pageNum: 1
          })
          this.selectedRowKeys = []
        })
      },
      // 导出PDF
      exportPDF() {
        console.log('导出PDF')
        this.$export('admissions/sszbzymlgl/export', this.queryParam)
      },
      exportBDF(){
         this.$export('admissions/sszbzymlgl/exportDbf', this.queryParam)
      },
      // 还原为上报
      handleReduction() {
        // console.log('还原为上报')
        // var deleteId = ''
        // if (!this.selectedRowKeys.length) {
        //   this.$message.warning('请选择需要还原上报状态的数据')
        //   return
        // }
        // if (this.selectedRows && this.selectedRows.length) {
        //   deleteId = this.selectedRows.map(v => {
        //     return v.id
        //   }).toString()
        // }
        // console.log(deleteId)
        var params = {ssyxm:this.queryParam.ssyxm,zsnd:this.queryParam.zsxn}
        var that = this;
        this.$confirm({
          title: '确定还原为上报状态?',
          onOk() {
            console.log('OK');
            that.$api.recruitstat.professCatalogManageReset(params).then(res => {
              // that.selectedRowKeys = []
              that.$message.success(res.msg)
              that.loadData({
                pageSize: 10,
                pageNum: that.pagination.current
              })
            })
          },
          onCancel() {
            console.log('Cancel');
          },
          class: 'test',
        });

      }
    }
  }
</script>

<style lang="less" scoped>
  .card {
    margin-bottom: 20px;
  }
</style>
